package com.example.wwk.graduationpractice.Activity;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.example.wwk.graduationpractice.Globalutils.ActivityCollector;
import com.example.wwk.graduationpractice.Globalutils.CustomDialogUtils;
import com.example.wwk.graduationpractice.Globalutils.MyApplication;
import com.example.wwk.graduationpractice.Globalutils.PromptShowUtils;
import com.example.wwk.graduationpractice.R;

import org.jivesoftware.smack.AbstractXMPPConnection;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smackx.iqregister.AccountManager;

public class SettingChangePwdActivity extends AppCompatActivity implements View.OnClickListener {
    //back
    private ImageView back;
    private Button sureButtn;
    //eidt
    private EditText newPwd;
    private EditText confirmNewPwd;
    //smack connection
    private AbstractXMPPConnection connection;
    //加载对话框
    private CustomDialogUtils progressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.setting_change_pwd_activity);
        setTitle("");
        ActivityCollector.addActivity(this);
        //back
        back = (ImageView) findViewById(R.id.back);
        back.setOnClickListener(this);
        //EditText
        newPwd = (EditText) findViewById(R.id.new_pwd);
        confirmNewPwd = (EditText) findViewById(R.id.confirm_new_pwd);
        //button
        sureButtn = (Button) findViewById(R.id.sure_button);
        sureButtn.setOnClickListener(this);
        //connection
        connection = MyApplication.getConnection();   //获取全局连接


    }

    /*
    判断新密码和确认密码是否符合要求
    @param String newPwd
    @param String confirmNewPwd
    return boolean
     */
    private boolean confirmNewPwd(String newPwd, String confirmNewPwd) {
        if (newPwd.equals("") || confirmNewPwd.equals(""))
            PromptShowUtils.showDialog2("警告", "密码输入不能为空！", this);
        else if (!newPwd.equals(confirmNewPwd))
            PromptShowUtils.showDialog2("警告", "两次密码输入不一致！", this);
        else
            return true;
        return false;
    }

    /*
    * 处理点击按钮的修改密码事件
    */
    private boolean dealButton(boolean flag, String confirmNewPwd) {
        if (flag) {
            AccountManager accountManager = AccountManager.getInstance(connection);
            try {
                //修改密码
                accountManager.changePassword(confirmNewPwd);
                return true;
            } catch (SmackException.NoResponseException e) {
                e.printStackTrace();
            } catch (XMPPException.XMPPErrorException e) {
                e.printStackTrace();
            } catch (SmackException.NotConnectedException e) {
                e.printStackTrace();
                PromptShowUtils.showDialog("警告", "网络不可达，请检查网络状态！", this);
            }
        }
        return false;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityCollector.removeActivity(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.sure_button:
                //显示加载框
                progressDialog = new CustomDialogUtils(this, R.style.CustomDialog);
                progressDialog.setCancelable(false);        //无法手动取消
                progressDialog.show();
                //获得edit内容
                String newPassword = newPwd.getText().toString();
                String confirmPassword = confirmNewPwd.getText().toString();
                boolean flag = confirmNewPwd(newPassword, confirmPassword); // 判断新密码和确认密码是否符合要求
                boolean dealed = dealButton(flag, confirmPassword);//处理点击按钮的修改密码是否成功
                progressDialog.dismiss();
                if (dealed)
                    PromptShowUtils.showDialog3("提示", "修改密码成功！", this);
                else
                    PromptShowUtils.showDialog2("提示", "修改密码失败！", this);
                break;
        }
    }

}
